// Do not remove the include below
#include "RF4RelayController.h"
#include <Command.h>

#include <VirtualWire.h>
//#include "Arduino.h"
#include "HardwareSerial.h"
#include <MemoryFree.h>

uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
char * commands[2]={"turnOn:1","turnOn:3:3"};
int c=0;

String sBuffer;

extern HardwareSerial Serial;
Relay relays[NUM_RELAYS];

int parse();

Relay createRelay(int port) {
	Relay result = Relay();
	result.port = port;
	result.isOn = false;
	pinMode(port, OUTPUT);
	return result;
}

void setup() {
	Serial.begin(9600);
	Serial.println("setup");

	relays[0] = createRelay(PORT_0);
	relays[1] = createRelay(PORT_1);
	relays[2] = createRelay(PORT_2);
	relays[3] = createRelay(PORT_3);

	vw_set_rx_pin(PORT_RF);
	vw_setup(4000); // Bits per sec
	vw_rx_start(); // Start the receiver PLL running
}

void loop2() {

	vw_wait_rx();
	if (vw_get_message(buf, &buflen)) // Non-blocking
			{
		sBuffer = String((char *) buf);
		Command command = Command::parseCommand(sBuffer);
		int relayID = command.getInteger(0);
		if(command.getName().equals(TURN_ON_CMD)){
			int relayID = command.getInteger(0);
			if (relayID!=-1) {
				Serial.print("turnOn(");
				Serial.print(command.getInteger(0));
				Serial.println(")");
				turnOn(relayID);
			}

		}
		else if (command.getName().equals(TURN_OFF_CMD)){

		}
	}
}

void loop() {
	String s = String(commands[c]);
	c++;
	if(c==2) c=0;
	Command command = Command::parseCommand(s);
	if(command.getName().equals(TURN_ON_CMD)){
		int relayID = command.getInteger(0);
		if (relayID!=-1) {
			Serial.print("turnOn(");
			Serial.print(command.getInteger(0));
			Serial.println(")");
			turnOn(relayID);
		}

	}
	else if (command.getName().equals(TURN_OFF_CMD)){

	}

	Serial.println(freeMemory());
	Serial.println("--------------------------");
	delay(1000);
}

int parse() {
	sBuffer = String((char *) buf);
	int port;
	if (sBuffer.equals("0")) {
		port = PORT_0;
	} else if (sBuffer.equals("1")) {
		port = PORT_1;
	} else if (sBuffer.equals("2")) {
		port = PORT_2;
	} else if (sBuffer.equals("3")) {
		port = PORT_3;
	} else
		port = -1;
	return port;
}

void turnOn(int id) {
	digitalWrite(relays[id].port, HIGH); // set the LED off
	relays[id].isOn = true;

}

void turnOff(int id) {
	digitalWrite(relays[id].port, LOW); // set the LED off
	relays[id].isOn = false;
}

void switchState(int id) {
	if (relays[id].isOn) {
		turnOff(id);
	} else {
		turnOn(id);
	}
}

